//
//  AssEvent.m
//  GrosoSub
//
//  Created by Josu López Fernández on 13/06/08.
//  Copyright 2008 Josu López Fernández. All rights reserved.
//
//	Redistribution and use in source and binary forms, with or without
//	modification, are permitted provided that the following conditions are met:
//
//		* Redistributions of source code must retain the above copyright notice,
//		  this list of conditions and the following disclaimer.
//		* Redistributions in binary form must reproduce the above copyright
//		  notice, this list of conditions and the following disclaimer in the
//		  documentation and/or other materials provided with the distribution.
//		* Neither the name of the library nor the names of its
//		  contributors may be used to endorse or promote products derived from
//		  this software without specific prior written permission.
//
//  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
//  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
//  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
//  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
//  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
//  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
//  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
//  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
//  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
//  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
//  POSSIBILITY OF SUCH DAMAGE.
//

#import "AssEvent.h"


@implementation AssEvent
	- (void) fillFromString:(NSString *) line
	{
		NSString *type, *st, *ed, *ml, *mr, *mv;
		NSScanner *scanner = [NSScanner scannerWithString:line];
		[scanner scanUpToString:@":" intoString:&type];
		if ([type isEqualToString:@"Dialogue"]) {
			dialogue = YES;
		} else {
			dialogue = NO;
		}
		[scanner scanString:@": " intoString:NULL]; //skip ": "
		
		[scanner scanInteger:&layer];
		[scanner scanString:@"," intoString:NULL]; //skip comma
		
		[scanner scanUpToString:@"," intoString:&st];
		[start fillFromString:st];
		[scanner scanString:@"," intoString:NULL]; //skip comma
		
		[scanner scanUpToString:@"," intoString:&ed];
		[end fillFromString:ed];
		[scanner scanString:@"," intoString:NULL]; //skip comma
		
		[scanner scanUpToString:@"," intoString:&style];
		[scanner scanString:@"," intoString:NULL]; //skip comma
		
		[scanner scanUpToString:@"," intoString:&name];
		[scanner scanString:@"," intoString:NULL]; //skip comma
		
		[scanner scanUpToString:@"," intoString:&ml];
		[marginL fillFromString:ml];
		[scanner scanString:@"," intoString:NULL]; //skip comma
		
		[scanner scanUpToString:@"," intoString:&mr];
		[marginR fillFromString:mr];
		[scanner scanString:@"," intoString:NULL]; //skip comma
		
		[scanner scanUpToString:@"," intoString:&mv];
		[marginV fillFromString:mv];
		[scanner scanString:@"," intoString:NULL]; //skip comma
		
		[scanner scanUpToString:@"," intoString:&effect];
		[scanner scanString:@"," intoString:NULL]; //skip comma
		
		[scanner scanUpToString:@"\n" intoString:&text];
	}
	
	- (NSString *) toString
	{
		if (dialogue == YES) {
			return [NSString stringWithFormat:@"Dialogue: %d,%@,%@,%@,%@,%@,%@,%@,%@,%@", layer,
				[start toString], [end toString], style, name, [marginL toString], [marginR toString],
				[marginV toString], effect, text];
		} else {
			return [NSString stringWithFormat:@"Comment: %d,%@,%@,%@,%@,%@,%@,%@,%@,%@", layer,
				[start toString], [end toString], style, name, [marginL toString], [marginR toString],
				[marginV toString], effect, text];
		}
	}
	
	- (BOOL) dialogue
	{
		return dialogue;
	}
	- (NSInteger) layer
	{
		return layer;
	}
	- (AssTime *) start
	{
		return start;
	}
	- (AssTime *) end
	{
		return end;
	}
	- (NSString *) style
	{
		return style;
	}
	- (NSString *) name
	{
		return name;
	}
	- (AssMargin *) marginL
	{
		return marginL;
	}
	- (AssMargin *) marginR
	{
		return marginR;
	}
	- (AssMargin *) marginV
	{
		return marginV;
	}
	- (NSString *) effect
	{
		return effect;
	}
	- (NSString *) text
	{
		return text;
	}

	- (void) setDialogue:(BOOL) di
	{
		dialogue = di;
	}
	- (void) setLayer:(NSInteger) la
	{
		layer = la;
	}
	- (void) setStart:(AssTime *) st
	{
		start = st;
	}
	- (void) setEnd:(AssTime *) ed
	{
		end = ed;
	}
	- (void) setStyle:(NSString *) st
	{
		style = st;
	}
	- (void) setName:(NSString *) na
	{
		name = na;
	}
	- (void) setMarginL:(AssMargin *) ml
	{
		marginL = ml;
	}
	- (void) setMarginR:(AssMargin *) mr
	{
		marginR = mr;
	}
	- (void) setMarginV:(AssMargin *) mv
	{
		marginV = mv;
	}
	- (void) setEffect:(NSString *) ef
	{
		effect = ef;
	}
	- (void) setText:(NSString *) te
	{
		text = te;
	}
	
	- init
	{
		if (self = [super init]) {
			start = [AssTime new];
			end = [AssTime new];
			marginL = [AssMargin new];
			marginR = [AssMargin new];
			marginV = [AssMargin new];
			style = @"";
			effect = @"";
			name = @"";
			text = @"";
		}
		return self;
	}
	- initWithString:(NSString *) string
	{
		if (self = [self init]) {
			[self fillFromString:string];
		}
		return self;
	}
@end
